package com.da.lon.wang.xlg.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.Classify3Adapter;
import com.da.lon.wang.xlg.bean.ResultSearchBean.ResultSearchListBean.SearchResultCategory.SearchResultCategoryItems;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class Classify3Activity extends BaseActivity implements OnClickListener {

    private Context context;

    private List<SearchResultCategoryItems> list = new ArrayList<SearchResultCategoryItems>();

    private ImageButton leftImageButton;
    private ListView listView;
    private Classify3Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ouka_classify3);

        context = this;

        Intent intent = getIntent();
        list = (List<SearchResultCategoryItems>) intent.getSerializableExtra("cateitemslist");
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void initView() {
        // TODO Auto-generated method stub


        leftImageButton = (ImageButton) findViewById(R.id.ouka_classify3_left);
        leftImageButton.setOnClickListener(this);

        listView = (ListView) findViewById(R.id.ouka_classify3_lv);
        adapter = new Classify3Adapter(list, context);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub

                String sattr = PreferenceUtils.getInstance().getSettingStr(PreferencesConfig.SEARCH_SATTR, "");

                String sattr1 = null;

                if (sattr.equals("")) {
                    if (list != null && list.get(position) != null) {
                        sattr1 = list.get(position).getName();
                    }
                } else {
                    if (list != null && list.get(position) != null) {
                        sattr1 = sattr + "," + list.get(position).getName();
                    }
                }

                PreferenceUtils.getInstance().SetSettingString(PreferencesConfig.SEARCH_SATTR, sattr1);
                if (list != null && list.get(position) != null) {
                    PreferenceUtils.getInstance().SetSettingString(PreferencesConfig.CLASSY_NAME,
                            list.get(position).getName());
                }

                finish();

                Classify2Activity.ActivityA.finish();

            }
        });


    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {

            case R.id.ouka_classify3_left:

                finish();

                break;
        }
    }
}
